package com.flong.gui.db;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;


@SuppressWarnings("all")


public class SQLServerPageTest extends JFrame implements MouseListener, ActionListener {
	public final String[] columnHeaders = { "ID(编号)", "Nmae(姓名)", "Sex(性别)" };
	JTable ab;
	JLabel ab1, ab2, ab3, ab4;
	JTextField test1, test2, test3, test01, test02, test03, test04;
	JButton bu, bu1, bu01, bu02, bu03, bu04;
	JScrollPane jsp;
	public String[][] str = null;
	SQLServer sz = new SQLServer();
	int y = 0;

	public SQLServerPageTest() {
		sz.sqlCont();
		setTitle("------------------");
		setBounds(100, 100, 500, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);
		bu = new JButton("下一页");
		bu.setBounds(415, 10, 75, 30);
		bu.addActionListener(this);
		bu1 = new JButton("上一页");
		bu1.setBounds(415, 50, 75, 30);
		bu1.addActionListener(this);
		SQLServer sz = new SQLServer();
		str = sz.selectSql(3, y);
		TableModel tc = new TableModel();

		ab = new JTable(tc);
		jsp = new JScrollPane(ab);
		jsp.setBounds(10, 10, 400, 80);
		ab.addMouseListener(this);
		test1 = new JTextField(20);
		test1.setBounds(20, 100, 100, 35);
		test2 = new JTextField(20);
		test2.setBounds(170, 100, 100, 35);
		test3 = new JTextField(20);
		test3.setBounds(320, 100, 100, 35);
		add(test1);
		add(bu);
		add(bu1);
		add(test2);
		add(test3);
		add(jsp);
		setVisible(true);
	}

	public static void main(String[] args) {
		new SQLServerPageTest();
	}

	public void mouseClicked(MouseEvent e) {
		test1.setText((String) ab.getValueAt(ab.getSelectedRow(), 0));
		test2.setText((String) ab.getValueAt(ab.getSelectedRow(), 1));
		test3.setText((String) ab.getValueAt(ab.getSelectedRow(), 2));
	}

	public void actionPerformed(ActionEvent e) {
		int x = sz.sqlCont();
		if (e.getActionCommand().equals("下一页")) {
			y++;
			if (y > x / 3) {
				JOptionPane.showMessageDialog(this, "没有下一页了");
				y--;
			} else {
				str = sz.selectSql(3, y);
				ab.updateUI();
			}
		} else if (e.getActionCommand().equals("上一页")) {
			y--;
			if (y < 0) {
				JOptionPane.showMessageDialog(this, "没有上一页了");
				y++;
			} else {
				str = sz.selectSql(3, y);
				ab.updateUI();
			}
		}
	}

	public class TableModel extends AbstractTableModel {
		public String getColumnName(int col) {
			return columnHeaders[col];
		}

		public int getColumnCount() {
			return str[0].length;
		}

		public int getRowCount() {
			return str.length;
		}

		public Object getValueAt(int row, int column) {
			return str[row][column];
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}
}